{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 快速入门\n",
    "\n",
    "此教程主要针对初级用户，内容主要包括利用MindVision的classification进行网络训练。\n",
    "\n",
    "## 基础知识\n",
    "\n",
    "图像分类顾名思义就是一个模式分类问题，是计算机视觉中最基础的任务，它的目标是将不同的图像，划分到不同的类别：\n",
    "\n",
    "- train/val/test dataset分别代表模型的训练集、验证集和测试集\n",
    "\n",
    "    - 训练集（train dataset）：用来训练模型，使模型能够识别不同类型的特征\n",
    "    - 验证集（val dataset）：训练过程中的测试集，方便训练过程中查看模型训练程度\n",
    "    - 测试集（test dataset）：训练模型结束后，用于评价模型结果的测试集\n",
    "\n",
    "- 迭代轮数（epoch）\n",
    "\n",
    "  模型训练迭代的总轮数，模型对训练集全部样本过一遍即为一个epoch。\n",
    "  当测试错误率和训练错误率相差较小时，可认为当前迭代轮数合适；\n",
    "  当测试错误率先变小后变大时，则说明迭代轮数过大，需要减小迭代轮数，否则容易出现过拟合。\n",
    "\n",
    "- 损失函数（Loss Function）\n",
    "\n",
    "  训练过程中，衡量模型输出（预测值）与真实值之间的差异\n",
    "\n",
    "- 准确率（Acc）\n",
    "\n",
    "  表示预测正确的样本数占总数据的比例\n",
    "\n",
    "## 环境安装与配置\n",
    "\n",
    "下载MindVision并进入文件夹"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "git clone https://gitee.com/mindspore/vision.git\n",
    "cd mindvision"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "安装"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "python setup.py install"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 数据的准备与处理\n",
    "\n",
    "进入`mindvision/classification`目录。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "cd mindvision/classification"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "下载并解压数据集.\n",
    "\n",
    "我们示例中用到的MNIST数据集是由10类28∗28的灰度图片组成，训练数据集包含60000张图片，测试数据集包含10000张图片。\n",
    "\n",
    "你可以从MNIST数据集下载页面下载，并按下方目录结构放置，或直接运行如下命令完成下载和放置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "!mkdir -p ./datasets/MNIST_Data/train ./datasets/MNIST_Data/test\n",
    "\n",
    "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte\n",
    "!wget -NP ./datasets/MNIST_Data/train https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte\n",
    "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte\n",
    "!wget -NP ./datasets/MNIST_Data/test https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!tree ./datasets/MNIST_Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "没有安装`wget`命令或者windows中下载的话，需要将地址拷贝到浏览器中下载，并进行解压。\n",
    "\n",
    "修改yaml文件中的路径\n",
    "\n",
    "yaml文件中有多个参数配置，其中`data_dir`是数据集路径，将本地数据集路径填入并保存。\n",
    "\n",
    "```text\n",
    "TRAIN:\n",
    "    batch_size: 32\n",
    "    num_workers: 1\n",
    "    dataset_name: \"mnist\"\n",
    "    data_dir: \"yourpath\"\n",
    "    shuffle: \"True\"\n",
    "    shuffle_seed: 0\n",
    "    transforms: None\n",
    "    loss: \"SoftmaxCrossEntropyWithLogits\"\n",
    "\n",
    "VALID:\n",
    "    batch_size: 32\n",
    "    num_workers: 1\n",
    "    dataset_name: \"mnist\"\n",
    "    data_dir: \"yourpath\"\n",
    "    shuffle: \"True\"\n",
    "    shuffle_seed: 0\n",
    "    transforms: None\n",
    "```\n",
    "\n",
    "返回``mindvision/classification``根目录"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "cd mindvision/classification"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 模型训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "python tools/train.py -c configs/lenet/lenet.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- `-c` 参数是指定训练的配置文件路径，训练的具体超参数可查看`yaml`文件\n",
    "- `yaml`文件中`epochs`参数设置为20，说明对整个数据集进行20个epoch迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 模型验证"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "python tools/eval.py -c configs/lenet/lenet.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- `-c` 参数是指定训练的配置文件路径，训练的具体超参数可查看`yaml`文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 模型推理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "python tools/eval.py -c configs/lenet/lenet.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- `-c` 参数是指定训练的配置文件路径，训练的具体超参数可查看`yaml`文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 模型导出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "python tools/eval.py -c configs/lenet/lenet.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- `-c` 参数是指定训练的配置文件路径，训练的具体超参数可查看`yaml`文件"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}